package clientapp;

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;


public class AddCredit extends JFrame implements ActionListener
{
	private JComboBox cmbCust = new JComboBox();
	private JLabel lblSelCust = new JLabel("Select customer to add credit to:");
	private JLabel lblAmount = new JLabel("Enter amount:");
	private JTextField txtCredit = new JTextField(4);
	private JButton btnAdd = new JButton("Add");
	private JButton btnCancel = new JButton("Cancel");
	
	/*depending on which item the user selects in combobox, eg.
	  array-index index 0, which is the the first combobox item, will
	  store the corresponding index in dvd/customers object array
	 */
	private String customers[];
	private String phone = new String();
	private String lastname = new String();
	private Client clientr;
	public AddCredit(Client clientp)
	{
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(400,150);
		setResizable(false);
		setTitle("Add Credit");
		setLocationRelativeTo(null);
		addWindowListener (new WindowAdapter ()
		{
            @Override
	    	public void windowClosing (WindowEvent e)
	    	{ //overiding windowClosing
				runStore.store.setEnabled(true);
	    	}
		} //the improved WindowListener is added.
		);
		
		//Client.sendQuery("AddCredit1");
		loadCustomerCombo();
		
		//layout
		setLayout(new BorderLayout());
		
		JPanel centerPanel = new JPanel(new FlowLayout());
		centerPanel.add(lblSelCust);
		centerPanel.add(cmbCust);
		centerPanel.add(lblAmount);
		centerPanel.add(txtCredit);
		
		JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		bottomPanel.add(btnAdd);
		bottomPanel.add(btnCancel);
		
		
		btnAdd.addActionListener(this);
		btnCancel.addActionListener(this);
		add(centerPanel,BorderLayout.CENTER);
		add(bottomPanel,BorderLayout.SOUTH);
		add(new JPanel(), BorderLayout.NORTH);
				
		setVisible(true);
	}
	
	private void loadCustomerCombo()
	{
		customers = new String[clientr.getResult().length];
			
		for (int i = 0; i < clientr.getResult().length; i++)
		{
			for(int j = 0; j < 2; j++)
			{
				phone = clientr.getResult()[i][0];
				lastname = clientr.getResult()[i][1];
			}
			String customer = phone + " " + lastname;
			customers[i] =  customer;
		}
		cmbCust = new JComboBox(customers);
	}
	
    @Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource()== btnAdd)
		{
			boolean creditGood = true;
			try
			{
				if ((Integer.parseInt(txtCredit.getText())) > 1000)
				{
					JOptionPane.showMessageDialog(null, "Please enter an amount less than 1000");
					creditGood = false;			
				}
				//Integer.parseInt(txtCredit.getText());
			}
			catch (NumberFormatException error)
			{
				JOptionPane.showMessageDialog(null, "Please enter an integer value only");
				creditGood = false;
			}
			if (creditGood == true)
			{
				String selected = (String)cmbCust.getSelectedItem();
				String number = selected.substring(0, 10);
				String credit = txtCredit.getText();
				if(credit != null)
				{
					String sendToServer = "AddCredit2" + "#" + number + "#" + credit;
					clientr.sendQuery(sendToServer);
					txtCredit.setText("");
				}
				
			}
			else
			{
				txtCredit.setText("");
				txtCredit.requestFocus();
			}
		}
		
		else if(e.getSource()==btnCancel)
		{
			//runStore.store.setEnabled(true);
			dispose();
		}
	}
}